#include "multi_button_callback.h"
#include "multi_button.h"
#include "bsp_key.h"
#include "bsp_power.h"
#include "lvgl_port.h"


Button btn_power;
Button btn_left;
Button btn_right;

uint8_t power_off_flag=false;


esp_err_t multi_button_callback_init(void)
{

	multi_button_callback_debug("初始化...");

	button_init(&btn_power, bsp_key_read_power_gpio, 1);
	button_start(&btn_power);
	button_init(&btn_left, ec11_task_get_left, 0);
	button_start(&btn_left);
	button_init(&btn_right, ec11_task_get_right, 0);
	button_start(&btn_right);

	button_attach(&btn_power, PRESS_DOWN, BTN_PRESS_DOWN_Handler);
	button_attach(&btn_power, PRESS_UP, BTN_PRESS_UP_Handler);
	button_attach(&btn_power, LONG_PRESS_START, BTN_LONG_PRESS_Handler);

	button_attach(&btn_left, PRESS_DOWN, BTN_PRESS_DOWN_Handler);

	button_attach(&btn_right, PRESS_DOWN, BTN_PRESS_DOWN_Handler);

	multi_button_callback_debug("初始化成功");
	return ESP_OK;
}


void BTN_LONG_PRESS_HOLD_Handler(void* btn)
{
	if(btn==&btn_power)
	{
		multi_button_callback_debug("power长按保持");
	}

}

void BTN_LONG_PRESS_Handler(void* btn)
{

	if(btn==&btn_power)
	{
		multi_button_callback_debug("power长按");
		bsp_key_data.power_long_press_flag = true;
		bsp_key_data.status = true;
		bsp_key_data.data = VK_ESCAPE;

	}
}

void BTN_PRESS_UP_Handler(void* btn)
{
	if(btn==&btn_power)
	{
		multi_button_callback_debug("power 抬起");
		if(bsp_key_data.power_long_press_flag == true)
		{
			multi_button_callback_debug("power 长按抬起");
			bsp_key_data.power_long_press_flag = false;
			bsp_key_data.status = true;
			bsp_key_data.data = VK_HOME;
			return;
		}

		multi_button_callback_debug("power up");
		bsp_key_data.status = true;
		bsp_key_data.data = VK_RETURN;

	}
}

void BTN_PRESS_DOWN_Handler(void* btn)
{
	if(btn==&btn_power)
	{
		if(bsp_key_data.power_long_press_flag == true)
		{
			bsp_key_data.power_long_press_flag = false;
			return;
		}

		bsp_key_data.status = true;
		bsp_key_data.data = VK_END;


		multi_button_callback_debug("power press");


	}else
	if(btn==&btn_left)
	{
		multi_button_callback_debug("left press");
		bsp_key_data.status = true;
		bsp_key_data.data = VK_LEFT;
	}else
	if(btn==&btn_right)
	{
		multi_button_callback_debug("right press");
		bsp_key_data.status = true;
		bsp_key_data.data = VK_RIGHT;
	}

}


